/**
 * Created by walkman on 2017/6/23.
 */
var app = angular.module('WfbPlatformApp', []);
app.controller('RCartController', function ($rootScope, $scope, $http, $timeout, $log) {
  $scope.$on('$viewContentLoaded', function () {
    // initialize core components
  });

  var postUrl = "/transaction/getPoints";
  var pointType = 9;
//设置当前选中页样式
  $scope.isActivePage = function (page) {
    return $scope.selPage == page;
  };
  $scope.pageSize = 10;
  $scope.selPage = 1;
  post(postUrl, pointType, $scope.selPage, $scope.pageSize);

  function post(apiUrl, pointType, pageIdx, pageSize) {
    var pageIdx = pageIdx || 1;
    var pageSize = pageSize || 10;
    jQuery.ajax({
      url: apiUrl,
      type: 'post',
      async: false,
      data: {
        pageNo: pageIdx,
        pageSize: pageSize,
        pointType: pointType
      },
      success: function (json) {
        $scope.contents = json.dataList;
        $scope.pages = Math.ceil(json.totalCount / $scope.pageSize); //分页数
        $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
        $scope.pageList = [];
        //分页要repeat的数组
        for (var i = 0; i < $scope.newPages; i++) {
          $scope.pageList.push(i + 1);
        }
        //console.log($scope.pageList);
        //console.log(pageIdx);
        $scope.isActivePage(pageIdx);
      }
    });
  }

  //打印当前选中页索引
  $scope.selectPage = function (page) {
    //不能小于1大于最大
    if (page < 1 || page > $scope.pages) return;
    //最多显示分页数5
    if (page > 2) {
      //因为只显示5个页数，大于2页开始分页转换
      var newpageList = [];
      for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)); i++) {
        newpageList.push(i + 1);
      }
      $scope.pageList = newpageList;
    }
    $scope.selPage = page;
    post(postUrl, pointType, $scope.selPage, $scope.pageSize);
  };

  //上一页
  $scope.Previous = function () {
    $scope.selectPage($scope.selPage - 1);
  }
  //下一页
  $scope.Next = function () {
    $scope.selectPage($scope.selPage + 1);
  };
});
